//index.js
//获取应用实例
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
const app = getApp();
var longitude = '';
var latitude = '';

Page({
    data: {
        isShowSkeletonLoding: true,
        everydayRecommend: [],
        goodsTypeList: [],
        mySpecialList: [],
        offlineGoodsList: []
    },
    onLoad: function () {
        wx.getSystemInfo({
            success: function (res) {
                app.globalData['statusBarHeight'] = res.statusBarHeight;
                console.log(res);
            }
        })
        if (!app.globalData.userID) {    //未检测到  wx.login是异步函数、此时可能获取不到值
            //创建 app.plumSessionCallback 的回调函数，将plumSession作为函数参数，app.js中检测到函数被创建，
            app.plumSessionCallback = userID => {
                if (userID != '' && userID != null) {
                    this.getHomePageData();
                } else {
                    app.weChatLogin();
                }
            }
        } else {
            this.getHomePageData();
        }
    },
    onShow: function () {
        if (this.data.offlineGoodsList.length == 0) {
            // 实例化API核心类
            qqmapsdk = new QQMapWX({
                //此key需要用户自己申请
                key: '3CSBZ-MMELI-LVRG2-5WUHP-7EU4T-CDF7A'
            });
            var that = this;
            // 调用接口
            qqmapsdk.reverseGeocoder({
                success: function (res) {
                    console.log(res);
                    longitude = res.result.location.lng;
                    latitude = res.result.location.lat;
                    app.globalData['longitude'] = longitude;
                    app.globalData['latitude'] = latitude;
                    that.setData({
                        longitude: longitude,
                        latitude: latitude
                    });
                    that.getOfflineGoodsData(latitude, longitude);
                },
                fail: function (res) {
                    wx.getSetting({
                        success: function (res) {
                            if (!res.authSetting['scope.userLocation']) {
                                wx.showModal({
                                    title: '提示',
                                    content: '请允许脉络购获取您的定位',
                                    confirmText: '授权',
                                    success(res) {
                                        if (res.confirm) {
                                            wx.openSetting({
                                                success(res) {
                                                    res.authSetting = {
                                                        "scope.userInfo": true,
                                                        "scope.userLocation": true
                                                    }
                                                }
                                            })
                                        } else if (res.cancel) {
                                            console.log('用户点击取消')
                                        }
                                    }
                                })
                            } else {
                                //用户已授权，但是获取地理位置失败，提示用户去系统设置中打开定位
                                wx.showModal({
                                    title: '提示',
                                    content: '请在系统设置中打开定位服务',
                                    confirmText: '确定',
                                    success: function (res) {
                                    }
                                })
                            }
                        }
                    });
                },
                complete: function (res) {
                    //console.log(res);
                }
            });
        }
    },
    onShareAppMessage: function (res) {
        
    },
    toGoodsTypePage: function(e) {
        var _type = e.currentTarget.dataset.type;
        var info = e.currentTarget.dataset.info;
        if (_type == 4) {
            wx.navigateTo({
                url: '../seckill/seckill',
            })
        } else {
            if (info.isOnline == 1) {
                wx.navigateTo({
                    url: '../goodsList/goodsList?goodsTypeID=' + info.goodsTypeID + '&goodsTypeName=' + info.goodsTypeName,
                })
            } else {
                wx.navigateTo({
                    url: '../offlineGoodsList/offlineGoodsList?goodsTypeID=' + info.goodsTypeID + '&goodsTypeName=' + info.goodsTypeName + '&longitude=' + this.data.longitude + '&latitude=' + this.data.latitude,
                })
            }
        }
    },
    getHomePageData: function () {
        var that = this;
        // request
        var params = {
            userID: app.globalData.userID
        }
        app.network.requestPost(app.route.ROUTE_GET_HOME_PAGE_DATA, params, '正在获取数据', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                var _everydayRecommend = res.context.everydayRecommend;
                var _goodsTypeList = res.context.goodsTypeList;
                var _mySpecialList = res.context.mySpecialList;
                that.setData({
                    isShowSkeletonLoding: false,
                    everydayRecommend: _everydayRecommend,
                    goodsTypeList: _goodsTypeList,
                    mySpecialList: _mySpecialList,
                });
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    },

    getOfflineGoodsData: function (latitude, longitude) {
        var that = this;
        var params = {
            lat: latitude,
            lng: longitude
        }
        app.network.requestPost(app.route.ROUTE_GET_OFFLINE_GOODS, params, '', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                var _mySpecialList = res.context.mySpecialList;
                that.setData({
                    offlineGoodsList: _mySpecialList
                });
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    },

    toGoodsDetail: function(e) {
        var _goodsId = e.currentTarget.dataset.goodsid;
        wx.navigateTo({
            url: '../goodsDetail/goodsDetail?goodsId=' + _goodsId,
        })
    },
    bannerTap: function(e) {
        var _info = e.currentTarget.dataset.info;
        if (_info.type == 1) { //为1时跳转到商品详情页面 其他则是自定义链接
            wx.navigateTo({
                url: '../goodsDetail/goodsDetail?goodsId=' + _info.goodsId,
            })
        } else {
            wx.navigateTo({
                url: '../webPage/webPage?url=' + encodeURIComponent(_info.url)
            })
        }
    },

    goSearchTab: function () {
        wx.navigateTo({
            url: '../search/search?longitude=' + this.data.longitude + '&latitude=' + this.data.latitude
        })
    }
})
